/*     */ package net.sourceforge.appgen.wizard;
/*     */ 
/*     */ import java.util.regex.Matcher;
/*     */ import java.util.regex.Pattern;
/*     */ import org.eclipse.core.resources.IContainer;
/*     */ import org.eclipse.core.resources.IResource;
/*     */ import org.eclipse.core.resources.IWorkspace;
/*     */ import org.eclipse.core.resources.IWorkspaceRoot;
/*     */ import org.eclipse.core.resources.ResourcesPlugin;
/*     */ import org.eclipse.core.runtime.IPath;
/*     */ import org.eclipse.core.runtime.Path;
/*     */ import org.eclipse.jface.viewers.ISelection;
/*     */ import org.eclipse.jface.viewers.IStructuredSelection;
/*     */ import org.eclipse.jface.wizard.WizardPage;
/*     */ import org.eclipse.swt.events.ModifyEvent;
/*     */ import org.eclipse.swt.events.ModifyListener;
/*     */ import org.eclipse.swt.events.SelectionAdapter;
/*     */ import org.eclipse.swt.events.SelectionEvent;
/*     */ import org.eclipse.swt.layout.GridData;
/*     */ import org.eclipse.swt.layout.GridLayout;
/*     */ import org.eclipse.swt.widgets.Button;
/*     */ import org.eclipse.swt.widgets.Composite;
/*     */ import org.eclipse.swt.widgets.Label;
/*     */ import org.eclipse.swt.widgets.Text;
/*     */ import org.eclipse.ui.dialogs.ContainerSelectionDialog;
/*     */ 
/*     */ public class MappingDataPage extends WizardPage
/*     */ {
/*     */   private Text containerText;
/*     */   private Text fileText;
/*     */   private ISelection selection;
/*     */ 
/*     */   public MappingDataPage(ISelection selection)
/*     */   {
/*  54 */     super("wizardPage");
/*  55 */     setTitle("AppGen Mapping Data File");
/*  56 */     setDescription("Create a new AppGen mapping data file with *.xml extension that can be edited by a AppGen mapping data editor.");
/*  57 */     this.selection = selection;
/*     */   }
/*     */ 
/*     */   public void createControl(Composite parent) {
/*  61 */     Composite container = new Composite(parent, 0);
/*  62 */     GridLayout layout = new GridLayout();
/*  63 */     container.setLayout(layout);
/*  64 */     layout.numColumns = 3;
/*  65 */     layout.verticalSpacing = 9;
/*  66 */     Label label = new Label(container, 0);
/*  67 */     label.setText("&Container:");
/*     */ 
/*  69 */     this.containerText = new Text(container, 2052);
/*  70 */     GridData gd = new GridData(768);
/*  71 */     this.containerText.setLayoutData(gd);
/*  72 */     this.containerText.addModifyListener(new ModifyListener() {
/*     */       public void modifyText(ModifyEvent e) {
/*  74 */         MappingDataPage.this.dialogChanged();
/*     */       }
/*     */     });
/*  78 */     Button button = new Button(container, 8);
/*  79 */     button.setText("Browse...");
/*  80 */     button.addSelectionListener(new SelectionAdapter()
/*     */     {
/*     */       public void widgetSelected(SelectionEvent e) {
/*  83 */         MappingDataPage.this.handleBrowse();
/*     */       }
/*     */     });
/*  86 */     label = new Label(container, 0);
/*  87 */     label.setText("&File name:");
/*     */ 
/*  89 */     this.fileText = new Text(container, 2052);
/*  90 */     gd = new GridData(768);
/*  91 */     this.fileText.setLayoutData(gd);
/*  92 */     this.fileText.addModifyListener(new ModifyListener() {
/*     */       public void modifyText(ModifyEvent e) {
/*  94 */         MappingDataPage.this.dialogChanged();
/*     */       }
/*     */     });
/*  97 */     initialize();
/*  98 */     dialogChanged();
/*  99 */     setControl(container);
/*     */   }
/*     */ 
/*     */   private void initialize() {
/* 103 */     if ((this.selection != null) && (!this.selection.isEmpty()) && ((this.selection instanceof IStructuredSelection))) {
/* 104 */       IStructuredSelection ssel = (IStructuredSelection)this.selection;
/* 105 */       if (ssel.size() > 1)
/* 106 */         return;
/* 107 */       Object obj = ssel.getFirstElement();
/* 108 */       if ((obj instanceof IResource))
/*     */       {
/*     */         IContainer container;
/* 110 */         if ((obj instanceof IContainer))
/* 111 */           container = (IContainer)obj;
/*     */         else
/* 113 */           container = ((IResource)obj).getParent();
/* 114 */         this.containerText.setText(container.getFullPath().toString());
/*     */       }
/*     */     }
/* 117 */     this.fileText.setText("appgen-mapping.xml");
/*     */   }
/*     */ 
/*     */   private void handleBrowse() {
/* 121 */     ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(), false, "Select new file container");
/* 122 */     if (dialog.open() == 0) {
/* 123 */       Object[] result = dialog.getResult();
/* 124 */       if (result.length == 1)
/* 125 */         this.containerText.setText(((Path)result[0]).toString());
/*     */     }
/*     */   }
/*     */ 
/*     */   private void dialogChanged()
/*     */   {
/* 131 */     IResource container = ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(getContainerName()));
/* 132 */     String fileName = getFileName();
/* 133 */     if (getContainerName().length() == 0) {
/* 134 */       updateStatus("File container must be specified");
/* 135 */       return;
/*     */     }
/* 137 */     if ((container == null) || ((container.getType() & 0x6) == 0)) {
/* 138 */       updateStatus("File container must exist");
/* 139 */       return;
/*     */     }
/* 141 */     if (!container.isAccessible()) {
/* 142 */       updateStatus("Project must be writable");
/* 143 */       return;
/*     */     }
/* 145 */     if (fileName.length() == 0) {
/* 146 */       updateStatus("File name must be specified");
/* 147 */       return;
/*     */     }
/* 149 */     int dotLoc = fileName.lastIndexOf('.');
/* 150 */     boolean validExtension = false;
/* 151 */     if (dotLoc != -1) {
/* 152 */       String ext = fileName.substring(dotLoc + 1);
/* 153 */       if (ext.equalsIgnoreCase("xml")) {
/* 154 */         validExtension = true;
/*     */       }
/*     */     }
/* 157 */     if (!validExtension) {
/* 158 */       updateStatus("File extension must be \"xml\"");
/* 159 */       return;
/*     */     }
/*     */ 
/* 162 */     String patternExp = "^[^\\/:*?\\\"<>\\|\\\\]+$";
/* 163 */     Pattern pattern = Pattern.compile(patternExp);
/* 164 */     Matcher matcher = pattern.matcher(fileName);
/* 165 */     if (!matcher.matches()) {
/* 166 */       updateStatus("File name invalid");
/* 167 */       return;
/*     */     }
/* 169 */     if ((fileName != null) && (fileName.length() > 0)) {
/* 170 */       IResource file = ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(getContainerName() + "/" + fileName));
/* 171 */       if (file != null) {
/* 172 */         updateStatus("File already exist");
/* 173 */         return;
/*     */       }
/*     */     }
/*     */ 
/* 177 */     updateStatus(null);
/*     */   }
/*     */ 
/*     */   private void updateStatus(String message) {
/* 181 */     setErrorMessage(message);
/* 182 */     setPageComplete(message == null);
/*     */   }
/*     */ 
/*     */   public String getContainerName() {
/* 186 */     return this.containerText.getText();
/*     */   }
/*     */ 
/*     */   public String getFileName() {
/* 190 */     return this.fileText.getText();
/*     */   }
/*     */ }

/* Location:           C:\Users\lzl6066\Downloads\net.sourceforge.appgen_1.2.0\plugins\net.sourceforge.appgen_1.2.0.201002031718\
 * Qualified Name:     net.sourceforge.appgen.wizard.MappingDataPage
 * JD-Core Version:    0.6.0
 */